<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>C++20 Coroutines Support</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../composition.html" title="Composition and Completion Tokens">
<link rel="prev" href="futures.html" title="Futures">
<link rel="next" href="coro.html" title="Resumable C++ 20 Coroutines">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="futures.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="coro.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_asio.overview.composition.cpp20_coroutines"></a><a class="link" href="cpp20_coroutines.html" title="C++20 Coroutines Support">C++20
        Coroutines Support</a>
</h4></div></div></div>
<p>
          Support for C++20 Coroutines is provided via the <a class="link" href="../../reference/awaitable.html" title="awaitable"><code class="computeroutput"><span class="identifier">awaitable</span></code></a> class template, the
          <a class="link" href="../../reference/use_awaitable_t.html" title="use_awaitable_t"><code class="computeroutput"><span class="identifier">use_awaitable</span></code></a>
          completion token, and the <a class="link" href="../../reference/co_spawn.html" title="co_spawn"><code class="computeroutput"><span class="identifier">co_spawn</span><span class="special">()</span></code></a>
          function. These facilities allow programs to implement asynchronous logic
          in a synchronous manner, in conjunction with the <code class="computeroutput"><span class="identifier">co_await</span></code>
          keyword, as shown in the following example:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">co_spawn</span><span class="special">(</span><span class="identifier">executor</span><span class="special">,</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">socket</span><span class="special">)),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detached</span><span class="special">);</span>

<span class="comment">// ...</span>

<span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">socket</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">try</span>
  <span class="special">{</span>
    <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
    <span class="keyword">for</span> <span class="special">(;;)</span>
    <span class="special">{</span>
      <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">));</span>
      <span class="identifier">co_await</span> <span class="identifier">async_write</span><span class="special">(</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">,</span> <span class="identifier">n</span><span class="special">));</span>
    <span class="special">}</span>
  <span class="special">}</span>
  <span class="keyword">catch</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">printf</span><span class="special">(</span><span class="string">"echo Exception: %s\n"</span><span class="special">,</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">());</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          The first argument to <code class="computeroutput"><span class="identifier">co_spawn</span><span class="special">()</span></code> is an <a class="link" href="../../reference/Executor1.html" title="Executor requirements">executor</a>
          that determines the context in which the coroutine is permitted to execute.
          For example, a server's per-client object may consist of multiple coroutines;
          they should all run on the same <code class="computeroutput"><span class="identifier">strand</span></code>
          so that no explicit synchronisation is required.
        </p>
<p>
          The second argument is an <a class="link" href="../../reference/awaitable.html" title="awaitable"><code class="computeroutput"><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span></code></a>,
          that is the result of the coroutine's entry point function, and in the
          above example is the result of the call to <code class="computeroutput"><span class="identifier">echo</span></code>.
          (Alternatively, this argument can be a function object that returns the
          <a class="link" href="../../reference/awaitable.html" title="awaitable"><code class="computeroutput"><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;</span></code></a>.) The template parameter <code class="computeroutput"><span class="identifier">R</span></code> is the type of return value produced
          by the coroutine. In the above example, the coroutine returns <code class="computeroutput"><span class="keyword">void</span></code>.
        </p>
<p>
          The third argument is a completion token, and this is used by <code class="computeroutput"><span class="identifier">co_spawn</span><span class="special">()</span></code>
          to produce a completion handler with signature <code class="computeroutput"><span class="keyword">void</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span><span class="special">,</span> <span class="identifier">R</span><span class="special">)</span></code>.
          This completion handler is invoked with the result of the coroutine once
          it has finished. In the above example we pass a completion token type,
          <a class="link" href="../../reference/detached.html" title="detached"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">detached</span></code></a>,
          which is used to explicitly ignore the result of an asynchronous operation.
        </p>
<p>
          In this example the body of the coroutine is implemented in the <code class="computeroutput"><span class="identifier">echo</span></code> function. When an asynchronous operation
          is called without explicitly specifying a completion token, the default
          completion token <a class="link" href="../../reference/deferred.html" title="deferred"><code class="computeroutput"><span class="identifier">deferred</span></code></a> is used. This causes
          the operation's initiating function to return a deferred asynchronous operation
          object that may be used with the <code class="computeroutput"><span class="identifier">co_await</span></code>
          keyword:
        </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">));</span>
</pre>
<p>
          Alternatively, we can specify the <code class="computeroutput"><span class="identifier">use_awaitable</span></code>
          completion token:
        </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_awaitable</span><span class="special">);</span>
</pre>
<p>
          With either of those completion tokens, when an asynchronous operation's
          handler signature has the form:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">,</span> <span class="identifier">result_type</span> <span class="identifier">result</span><span class="special">);</span>
</pre>
<p>
          the resulting type of the <code class="computeroutput"><span class="identifier">co_await</span></code>
          expression is <code class="computeroutput"><span class="identifier">result_type</span></code>.
          In the <code class="computeroutput"><span class="identifier">async_read_some</span></code>
          example above, this is <code class="computeroutput"><span class="identifier">size_t</span></code>.
          If the asynchronous operation fails, the <code class="computeroutput"><span class="identifier">error_code</span></code>
          is converted into a <code class="computeroutput"><span class="identifier">system_error</span></code>
          exception and thrown.
        </p>
<p>
          Where a handler signature has the form:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">);</span>
</pre>
<p>
          the <code class="computeroutput"><span class="identifier">co_await</span></code> expression
          produces a <code class="computeroutput"><span class="keyword">void</span></code> result. As
          above, an error is passed back to the coroutine as a <code class="computeroutput"><span class="identifier">system_error</span></code>
          exception.
        </p>
<h6>
<a name="boost_asio.overview.composition.cpp20_coroutines.h0"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.cpp20_coroutines.error_handling"></a></span><a class="link" href="cpp20_coroutines.html#boost_asio.overview.composition.cpp20_coroutines.error_handling">Error
          Handling</a>
        </h6>
<p>
          To perform explicit error handling, rather than the default exception-throwing
          behaviour, use the <a class="link" href="../../reference/as_tuple.html" title="as_tuple"><code class="computeroutput"><span class="identifier">as_tuple</span></code></a> or <a class="link" href="../../reference/redirect_error.html" title="redirect_error"><code class="computeroutput"><span class="identifier">redirect_error</span></code></a> completion token
          adapters.
        </p>
<p>
          The <code class="computeroutput"><span class="identifier">as_tuple</span></code> completion
          token adapter packages the completion handler arguments into a single tuple,
          which is then returned as the result of the awaited operation. For example:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">socket</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
  <span class="keyword">for</span> <span class="special">(;;)</span>
  <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">result</span> <span class="special">=</span>
      <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">as_tuple</span><span class="special">);</span>
    <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;(</span><span class="identifier">result</span><span class="special">))</span>
    <span class="special">{</span>
      <span class="comment">// success</span>
    <span class="special">}</span>

    <span class="comment">// ...</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          or if explicitly specifying the <code class="computeroutput"><span class="identifier">use_awaitable</span></code>
          completion token:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">socket</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
  <span class="keyword">for</span> <span class="special">(;;)</span>
  <span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">result</span> <span class="special">=</span>
      <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">),</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">as_tuple</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">use_awaitable</span><span class="special">));</span>
    <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;(</span><span class="identifier">result</span><span class="special">))</span>
    <span class="special">{</span>
      <span class="comment">// success</span>
    <span class="special">}</span>

    <span class="comment">// ...</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          The result can also be captured directly into a structured binding:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">socket</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
  <span class="keyword">for</span> <span class="special">(;;)</span>
  <span class="special">{</span>
    <span class="keyword">auto</span> <span class="special">[</span><span class="identifier">ec</span><span class="special">,</span> <span class="identifier">n</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">as_tuple</span><span class="special">);</span>
    <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// success</span>
    <span class="special">}</span>

    <span class="comment">// ...</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          Alternatively, the <code class="computeroutput"><span class="identifier">redirect_error</span></code>
          completion token adapter may be used to capture the error into a supplied
          <code class="computeroutput"><span class="identifier">error_code</span></code> variable:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">socket</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
  <span class="keyword">for</span> <span class="special">(;;)</span>
  <span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">redirect_error</span><span class="special">(</span><span class="identifier">ec</span><span class="special">));</span>
    <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
      <span class="comment">// success</span>
    <span class="special">}</span>

    <span class="comment">// ...</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.cpp20_coroutines.h1"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.cpp20_coroutines.coroutines_and_per_operation_cancellation"></a></span><a class="link" href="cpp20_coroutines.html#boost_asio.overview.composition.cpp20_coroutines.coroutines_and_per_operation_cancellation">Coroutines
          and Per-Operation Cancellation</a>
        </h6>
<p>
          All threads of execution created by <code class="computeroutput"><span class="identifier">co_spawn</span></code>
          have a cancellation state that records the current state of any cancellation
          requests made to the coroutine. To access this state, use <a class="link" href="../../reference/this_coro__cancellation_state.html" title="this_coro::cancellation_state"><code class="computeroutput"><span class="identifier">this_coro</span><span class="special">::</span><span class="identifier">cancellation_state</span></code></a> as follows:
        </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">awaitable</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span> <span class="identifier">my_coroutine</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">cancellation_state</span> <span class="identifier">cs</span>
    <span class="special">=</span> <span class="identifier">co_await</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">this_coro</span><span class="special">::</span><span class="identifier">cancellation_state</span><span class="special">;</span>

  <span class="comment">// ...</span>

  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">cs</span><span class="special">.</span><span class="identifier">cancelled</span><span class="special">()</span> <span class="special">!=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">cancellation_type</span><span class="special">::</span><span class="identifier">none</span><span class="special">)</span>
    <span class="comment">// ...</span>
<span class="special">}</span>
</pre>
<p>
          When first created by <code class="computeroutput"><span class="identifier">co_spawn</span></code>,
          the thread of execution has a cancellation state that supports <code class="computeroutput"><span class="identifier">cancellation_type</span><span class="special">::</span><span class="identifier">terminal</span></code> values only. To change the cancellation
          state, call <a class="link" href="../../reference/this_coro__reset_cancellation_state.html" title="this_coro::reset_cancellation_state"><code class="computeroutput"><span class="identifier">this_coro</span><span class="special">::</span><span class="identifier">reset_cancellation_state</span></code></a>.
        </p>
<p>
          By default, continued execution of a cancelled coroutine will trigger an
          exception from any subsequent <code class="computeroutput"><span class="identifier">co_await</span></code>
          of an <code class="computeroutput"><span class="identifier">awaitable</span><span class="special">&lt;&gt;</span></code>
          object. This behaviour can be changed by using <a class="link" href="../../reference/this_coro__throw_if_cancelled.html" title="this_coro::throw_if_cancelled"><code class="computeroutput"><span class="identifier">this_coro</span><span class="special">::</span><span class="identifier">throw_if_cancelled</span></code></a>.
        </p>
<h6>
<a name="boost_asio.overview.composition.cpp20_coroutines.h2"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.cpp20_coroutines.co_ordinating_parallel_coroutines"></a></span><a class="link" href="cpp20_coroutines.html#boost_asio.overview.composition.cpp20_coroutines.co_ordinating_parallel_coroutines">Co-ordinating
          Parallel Coroutines</a>
        </h6>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            This is an experimental feature.
          </p></td></tr>
</table></div>
<p>
          The logical operators <code class="computeroutput"><span class="special">||</span></code> and
          <code class="computeroutput"><span class="special">&amp;&amp;</span></code> have been overloaded
          for <code class="computeroutput"><span class="identifier">awaitable</span><span class="special">&lt;&gt;</span></code>,
          to allow coroutines to be trivially awaited in parallel.
        </p>
<p>
          When awaited using <code class="computeroutput"><span class="special">&amp;&amp;</span></code>,
          the <code class="computeroutput"><span class="identifier">co_await</span></code> expression
          waits until both operations have completed successfully. As a "short-circuit"
          evaluation, if one operation fails with an exception, the other is immediately
          cancelled. For example:
        </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">results</span> <span class="special">=</span>
  <span class="identifier">co_await</span> <span class="special">(</span>
    <span class="identifier">async_read</span><span class="special">(</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">input_buffer</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">)</span>
      <span class="special">&amp;&amp;</span> <span class="identifier">async_write</span><span class="special">(</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">output_buffer</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">)</span>
  <span class="special">);</span>
</pre>
<p>
          Following completion of a <code class="computeroutput"><span class="special">&amp;&amp;</span></code>
          operation, the results of all operations are concatenated into a tuple.
          In the above example, the first <code class="computeroutput"><span class="identifier">size_t</span></code>
          represents the non-exceptional component of the <code class="computeroutput"><span class="identifier">async_read</span></code>
          result, and the second <code class="computeroutput"><span class="identifier">size_t</span></code>
          is the result of the <code class="computeroutput"><span class="identifier">async_write</span></code>.
        </p>
<p>
          When awaited using <code class="computeroutput"><span class="special">||</span></code>, the
          <code class="computeroutput"><span class="identifier">co_await</span></code> expression waits
          until either operation succeeds. As a "short-circuit" evaluation,
          if one operation succeeds without throwing an exception, the other is immediately
          cancelled. For example:
        </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">monostate</span><span class="special">&gt;</span> <span class="identifier">results</span> <span class="special">=</span>
  <span class="identifier">co_await</span> <span class="special">(</span>
    <span class="identifier">async_read</span><span class="special">(</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">input_buffer</span><span class="special">,</span> <span class="identifier">use_awaitable</span><span class="special">)</span>
      <span class="special">||</span> <span class="identifier">timer</span><span class="special">.</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">use_awaitable</span><span class="special">)</span>
  <span class="special">);</span>
</pre>
<p>
          Following completion of a <code class="computeroutput"><span class="special">||</span></code>
          operation, the result of the first operation to complete non-exceptionally
          is placed into a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">variant</span></code>. The active index of the variant
          reflects which of the operations completed first. In the above example,
          index <code class="computeroutput"><span class="number">0</span></code> corresponds to the
          <code class="computeroutput"><span class="identifier">async_read</span></code> operation.
        </p>
<p>
          These operators may be enabled by adding the <code class="computeroutput"><span class="preprocessor">#include</span></code>:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">asio</span><span class="special">/</span><span class="identifier">experimental</span><span class="special">/</span><span class="identifier">awaitable_operators</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
          and then bringing the contents of the <code class="computeroutput"><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">awaitable_operators</span></code>
          namespace into scope:
        </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">experimental</span><span class="special">::</span><span class="identifier">awaitable_operators</span><span class="special">;</span>
</pre>
<p>
          Note: To use these operators we must explicitly specify the <code class="computeroutput"><span class="identifier">use_awaitable</span></code> completion token.
        </p>
<h6>
<a name="boost_asio.overview.composition.cpp20_coroutines.h3"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.cpp20_coroutines.lightweight_coroutines_implementing_asynchonous_operations"></a></span><a class="link" href="cpp20_coroutines.html#boost_asio.overview.composition.cpp20_coroutines.lightweight_coroutines_implementing_asynchonous_operations">Lightweight
          Coroutines Implementing Asynchonous Operations</a>
        </h6>
<p>
          The <code class="computeroutput"><span class="identifier">co_composed</span></code> template
          facilitates a lightweight implementation of user-defined asynchronous operations
          using C++20 coroutines. The following example illustrates a simple asynchronous
          operation that implements an echo protocol in terms of a coroutine:
        </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">CompletionToken</span><span class="special">&gt;</span>
<span class="keyword">auto</span> <span class="identifier">async_echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">&amp;</span> <span class="identifier">socket</span><span class="special">,</span>
    <span class="identifier">CompletionToken</span><span class="special">&amp;&amp;</span> <span class="identifier">token</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_initiate</span><span class="special">&lt;</span>
    <span class="identifier">CompletionToken</span><span class="special">,</span> <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)&gt;(</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">co_composed</span><span class="special">&lt;</span>
        <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)&gt;(</span>
          <span class="special">[](</span><span class="keyword">auto</span> <span class="identifier">state</span><span class="special">,</span> <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">&amp;</span> <span class="identifier">socket</span><span class="special">)</span> <span class="special">-&gt;</span> <span class="keyword">void</span>
          <span class="special">{</span>
            <span class="keyword">try</span>
            <span class="special">{</span>
              <span class="identifier">state</span><span class="special">.</span><span class="identifier">throw_if_cancelled</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>
              <span class="identifier">state</span><span class="special">.</span><span class="identifier">reset_cancellation_state</span><span class="special">(</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">enable_terminal_cancellation</span><span class="special">());</span>

              <span class="keyword">for</span> <span class="special">(;;)</span>
              <span class="special">{</span>
                <span class="keyword">char</span> <span class="identifier">data</span><span class="special">[</span><span class="number">1024</span><span class="special">];</span>
                <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span> <span class="special">=</span>
                  <span class="identifier">co_await</span> <span class="identifier">socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">));</span>

                <span class="identifier">co_await</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_write</span><span class="special">(</span><span class="identifier">socket</span><span class="special">,</span>
                    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">buffer</span><span class="special">(</span><span class="identifier">data</span><span class="special">,</span> <span class="identifier">n</span><span class="special">));</span>
              <span class="special">}</span>
            <span class="special">}</span>
            <span class="keyword">catch</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">system_error</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
            <span class="special">{</span>
              <span class="identifier">co_return</span> <span class="special">{</span><span class="identifier">e</span><span class="special">.</span><span class="identifier">code</span><span class="special">()};</span>
            <span class="special">}</span>
          <span class="special">},</span> <span class="identifier">socket</span><span class="special">),</span>
      <span class="identifier">token</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">socket</span><span class="special">));</span>
<span class="special">}</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.cpp20_coroutines.h4"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.cpp20_coroutines.see_also"></a></span><a class="link" href="cpp20_coroutines.html#boost_asio.overview.composition.cpp20_coroutines.see_also">See
          Also</a>
        </h6>
<p>
          <a class="link" href="../../reference/co_spawn.html" title="co_spawn">co_spawn</a>, <a class="link" href="../../reference/detached.html" title="detached">detached</a>,
          <a class="link" href="../../reference/as_tuple.html" title="as_tuple">as_tuple</a>, <a class="link" href="../../reference/redirect_error.html" title="redirect_error">redirect_error</a>,
          <a class="link" href="../../reference/awaitable.html" title="awaitable">awaitable</a>, <a class="link" href="../../reference/use_awaitable_t.html" title="use_awaitable_t">use_awaitable_t</a>,
          <a class="link" href="../../reference/use_awaitable.html" title="use_awaitable">use_awaitable</a>,
          <a class="link" href="../../reference/this_coro__executor.html" title="this_coro::executor">this_coro::executor</a>,
          <a class="link" href="../../reference/co_composed.html" title="co_composed">co_composed</a>, <a class="link" href="../../examples/cpp17_examples.html#boost_asio.examples.cpp17_examples.coroutines_ts_support">Coroutines
          examples</a>, <a class="link" href="coro.html" title="Resumable C++ 20 Coroutines">Resumable
          C++20 Coroutines</a>, <a class="link" href="spawn.html" title="Stackful Coroutines">Stackful
          Coroutines</a>, <a class="link" href="coroutine.html" title="Stackless Coroutines">Stackless
          Coroutines</a>.
        </p>
</div>
<div class="copyright-footer">Copyright © 2003-2025 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="futures.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="coro.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
